package com.example.iceman.stepcounterapp.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.example.iceman.stepcounterapp.DeviceUtil;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 自定义控件.以圆环形式显示当前步数
 * Created by iceman on 16/3/10 11:34
 * 邮箱：xubin865@pingan.com.cn
 */
public class CircleShapeView extends View {
    private int currentPercent = 0;
    private int mWidth;
    private int mHeight;
    private int lineWidth;
    private int[] colors = {0xffa5ea91, 0xffe4ee61, 0xfffab052, 0xfff9557a, 0xfffaa0b7, 0xffadddee, 0xff38cbf9, 0xff5ad5d5, 0xffa5ea91};
    private Point downPoint = new Point();

    private OnClickListener clickListener;

    private TimerTask timerTask;
    private Timer timer;

    public CircleShapeView(Context context) {
        super(context);
        initPaint();
    }

    public CircleShapeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    private void initPaint() {
        lineWidth = DeviceUtil.dip2px(30);
        paint = new Paint();
        paint.setStrokeWidth(lineWidth);
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        paint.setColor(0xffe7e7e7);
        paint.setStrokeCap(Paint.Cap.ROUND);
    }

    private SweepGradient gradient;
    private Paint paint;
    private RectF rectF;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(0xfff5f5f9);
        paint.setShader(null);
        canvas.drawArc(rectF, 0f, 360f, false, paint);
        paint.setShader(gradient);
        canvas.drawArc(rectF, -90f, 360f * currentPercent / 100, false, paint);


    }

    public void setOnclickListener(OnClickListener onclickListener) {
        this.clickListener = onclickListener;
    }

    public void animationToPrercent(final int percent) {
        if (timer != null) {
            timer.cancel();
        }
        timer = new Timer();
        timerTask = new TimerTask() {
            @Override
            public void run() {
                if (percent > currentPercent) {
                    currentPercent++;
                    postInvalidate();
                }
            }
        };
        timer.schedule(timerTask, 0, 20);

    }

    public void setCurrentPercent(int currentPercent) {
        this.currentPercent = currentPercent;
        invalidate();
    }

    @Override
    protected void onDetachedFromWindow() {
        if (timer != null) {
            timer.cancel();
        }
        super.onDetachedFromWindow();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightModel = MeasureSpec.getMode(heightMeasureSpec);
        int widthModel = MeasureSpec.getMode(widthMeasureSpec);
        if (heightModel == MeasureSpec.EXACTLY || widthModel == MeasureSpec.EXACTLY) {
            if (heightModel == MeasureSpec.EXACTLY) {
                int height = MeasureSpec.getSize(heightMeasureSpec);
                mHeight = height;
                mWidth = height;
            } else {
                int width = MeasureSpec.getSize(widthMeasureSpec);
                mWidth = width;
                mHeight = width;
            }
        } else {
            mHeight = 600;
            mWidth = 600;
        }
        gradient = new SweepGradient(mWidth / 2, mHeight / 2, colors, null);
        rectF = new RectF(lineWidth / 2, lineWidth / 2, mWidth - lineWidth / 2, mHeight - lineWidth / 2);
        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downPoint.x = (int) event.getX();
                downPoint.y = (int) event.getY();
                return true;
            case MotionEvent.ACTION_UP:
                int x = (int) event.getX();
                int y = (int) event.getY();
                int distence = (int) Math.sqrt(Math.pow((y - downPoint.y), 2) + Math.pow((x - downPoint.x), 2));
                if (distence < DeviceUtil.dip2px(20) && clickListener != null) {
                    clickListener.onClick(this);
                }
                break;
        }
        return super.onTouchEvent(event);
    }
}
